// 引入重置样式
require("../../assets/css/reset.css");
// 引入公共样式
require("../../assets/css/basic.css");
// 引入矢量图
require("../../assets/fonts/iconfont.css");
// 引入当前样式
require("./login.less");

// 引入dom
const dom = require("../../utils/dom.js");
// 引入axios模块
const axios = require("../../utils/axios.js");

window.addEventListener("DOMContentLoaded", () => {
    const error = dom.get("#error");  //提示文字

    // 表单验证
    dom.get("#btn").addEventListener("click", () => {
        // 账号验证
        const tel = dom.get("#tel").value;
        let regex = /^1[3456789]\d{9}$/;
        if(regex.test(tel)){
            error.textContent = "";
        }else{
            error.textContent = "账号输入错误";
            return;
        }

        // 密码验证
        const pwd = dom.get("#pwd").value;
        let regex2 = /^[A-Za-z0-9]{6,}$/;
        if(regex2.test(pwd)){
            error.textContent = "";
        }else{
            error.textContent = "账号密码错误";
            return;
        }

        // 验证成功
        axios.post("/api/user/login", {
            account:tel,
            password:pwd,
        }).then(response => {
            if(response.data.errno === 0){
                localStorage.setItem("token", response.data.data.token);  //存数token数据
                location.href = "index.html";
            }else{
                alert(response.data.message);
            }
        })
    })
})